"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.getRequestConfig = undefined;

var _extends2 = require("babel-runtime/helpers/extends");

var _extends3 = _interopRequireDefault(_extends2);

var _setQuery = require("../setQuery");

var _safeStringify = require("../safeStringify");

var _safeStringify2 = _interopRequireDefault(_safeStringify);

var _urlEncode = require("../urlEncode");

var _urlEncode2 = _interopRequireDefault(_urlEncode);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var getRequestConfig = exports.getRequestConfig = function getRequestConfig() {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        url = _ref.url,
        _ref$method = _ref.method,
        method = _ref$method === undefined ? 'get' : _ref$method,
        _ref$postType = _ref.postType,
        postType = _ref$postType === undefined ? 'url' : _ref$postType,
        _ref$data = _ref.data,
        data = _ref$data === undefined ? {} : _ref$data,
        _ref$body = _ref.body,
        body = _ref$body === undefined ? {} : _ref$body,
        _ref$headers = _ref.headers,
        headers = _ref$headers === undefined ? {} : _ref$headers,
        _ref$withCredentials = _ref.withCredentials,
        withCredentials = _ref$withCredentials === undefined ? true : _ref$withCredentials;

    var _method = method.toUpperCase();
    var config = {
        body: body,
        method: _method,
        headers: (0, _extends3.default)({}, headers)
    };

    var _url = void 0;
    if (_method === 'GET') {
        _url = (0, _setQuery.setQuery)(url, (0, _extends3.default)({}, data, body));
        delete config.body;
    } else {
        _url = (0, _setQuery.setQuery)(url, data);
    }
    config.url = _url;

    if (_method === 'POST') {
        setPostData(config, postType);
    }

    config.mode = 'cors';
    if (withCredentials) {
        config.credentials = 'include';
    }
    return config;
};

var setPostData = function setPostData(config, postType) {
    var _headers = config.headers;
    if (postType === 'json') {
        _headers['Content-Type'] = 'application/json';
        config.body = (0, _safeStringify2.default)(config.body);
    } else if (postType === 'url') {
        _headers['Content-Type'] = 'application/x-www-form-urlencoded';
        config.body = (0, _urlEncode2.default)(config.body);
    }
};
exports.default = getRequestConfig;